<?php

namespace Admin\Controller;

use Common\Lib\Weixin;

class WeixinController extends CommonController
{
    // 公众号配置
    public function config()
    {
        $show = ytata_get_cache('config');
        $this->assign('show', $show);
        $this->display();
    }

    // 保存公众号配置
    public function save_config()
    {
        $config = I('post.');
        foreach ($config as $name => $value) {
            $map['name']   = $name;
            $save['value'] = $value;
            M('Config')->where($map)->save($save);
            ytata_refresh_cache('config');
        }
        $this->success('保存成功');
    }

    public function other()
    {
        $show = ytata_get_cache('config');

        $this->assign(compact('show'));

        $this->display();
    }

    // 微信底部菜单
    public function tabbar()
    {
        $data = D("WeixinTabbar");
        $list = $data->order('sort DESC, id ASC')->select();
        $this->assign('list', $list);
        $this->display();
    }

    // 保存底部菜单
    public function tabbar_save()
    {
        $data = I('post.');

        $data = array_form_input($data);
        $num = 0;
        foreach ($data as $datum) {
            if ($datum['display'] == '1') {
                $num++;
            }
        }
        if ($num < 3 || $num > 5) {
            $this->error('底部菜单仅支持3-5个');
        }

        D('WeixinTabbar')->tabbar_edit($data);
        $this->success('保存成功');
    }

    // 菜单列表
    public function menu_list()
    {
        if (IS_AJAX) {
            $pid = I('get.pid', '', 'intval');

            $map['pid'] = $pid;
            $content    = D('WeixinMenu')->where($map)->order('sort desc, id')->select();
            $content    = array_map(function ($menu) {
                $menu['editurl'] = U('Weixin/menu_edit', array('id' => $menu['id']));
                $menu['delurl']  = U('Weixin/menu_del', array('id' => $menu['id']));
                return $menu;
            }, $content);
            $status     = 1;

            $this->ajaxReturn(compact('status', 'content'));
        }
        $map['pid'] = 0;
        $list       = D('WeixinMenu')->where($map)->order('sort desc, id')->select();

        $list = array_map(function ($menu) {
            $menu['editurl'] = U('Weixin/menu_edit', array('id' => $menu['id']));
            $menu['delurl']  = U('Weixin/menu_del', array('id' => $menu['id']));
            return $menu;
        }, $list);

        $this->assign(compact('list'));

        $this->display();
    }

    // 添加菜单
    public function menu_add()
    {
        // 一级菜单数量限制 最多三个
        $status = 1;
        $pid    = I('param.pid', '0', 'intval');
        $count  = M('WeixinMenu')->where(compact('status', 'pid'))->count();
        if ($pid === '0') {
            if ($count >= 3) {
                $this->error('最多只能启用3个一级菜单', U('menu_list'));
            }
        } else {
            if ($count >= 5) {
                $this->error('最多只能启用5个二级菜单', U('menu_list'));
            }
        }

        if (IS_POST) {
            $rules = array(
                array('name', 'require', '菜单名称必须填写'),
                array('pid', 'require', '所属父菜单必须填写'),
                array('type', 'require', '菜单类型必须填写'),
            );
            $type  = I('post.type');
            if ($type == 'view') {
                $rules[] = array('url', 'require', 'URL必须填写');
//                $rules[] = array('url', 'url', 'URL格式错误');
            } elseif ($type == 'click') {
                $rules[] = array('key', 'require', '关键词必须填写');
//                $rules[] = array('key', '1,40', '关键词长度限制', 0, 'length');
            }

            $weixin_menu = D('WeixinMenu');
            if (!$weixin_menu->validate($rules)->create()) {
                $this->error($weixin_menu->getError());
            } else {
                $weixin_menu->add();
                $this->success('添加成功', U('menu_list'));
            }
        } else {
            $pid    = I('get.pid', 0, 'intval');
            $pid_cn = D('WeixinMenu')->where(array('id' => $pid))->getField('name');

            $map['pid'] = 0;
            $menulist   = D('WeixinMenu')->where($map)->select();

            $this->assign(compact('menulist', 'pid', 'pid_cn'));

            $this->display();
        }
    }

    // 编辑菜单
    public function menu_edit()
    {
        if (IS_POST) {
            // 当启用菜单时判断是否启用限制
            if (I('post.status') == '1') {
                // 一级菜单数量限制 最多三个
                $status = 1;
                $pid    = I('post.pid', '0', 'intval');
                $count  = M('WeixinMenu')->where(compact('status', 'pid'))->count();
                if ($pid === '0') {
                    if ($count >= 3) {
                        $this->error('最多只能启用3个一级菜单', U('menu_list'));
                    }
                } else {
                    if ($count >= 5) {
                        $this->error('最多只能启用5个二级菜单', U('menu_list'));
                    }
                }
            }

            $rules = array(
                array('name', 'require', '菜单名称必须填写'),
                array('pid', 'require', '所属父菜单必须填写'),
                array('type', 'require', '菜单类型必须填写'),
            );
            $type  = I('post.type');
            if ($type == 'view') {
                $rules[] = array('url', 'require', 'URL必须填写');
//                $rules[] = array('url', 'url', 'URL格式错误');
            } elseif ($type == 'click') {
                $rules[] = array('key', 'require', '关键词必须填写');
//                $rules[] = array('key', '1,40', '关键词长度限制', 0, 'length');
            }

            $menu = D('WeixinMenu');
            if (!$menu->validate($rules)->create()) {
                $this->error($menu->getError());
            } else {
                $menu->save();
                $this->success('保存成功', 'menu_list');
            }
        } else {
            $menu_id = I('get.id', '', 'intval');
            if ($menu_id) {
                $menu = D('WeixinMenu')->find($menu_id);

                $map['pid'] = 0;
                $menulist   = D('WeixinMenu')->where($map)->select();

                $pid    = $menu['pid'];
                $pid_cn = D('WeixinMenu')->where(array('id' => $pid))->getField('name') ?: '顶级菜单';

                $this->assign(compact('menulist', 'menu', 'pid', 'pid_cn'));

                $this->display();
            } else {
                $this->error('非法请求');
            }
        }
    }

    // 删除菜单
    public function menu_del()
    {
        $menu_id = I('get.id', '', 'intval');
        if ($menu_id) {
            D('WeixinMenu')->menu_del($menu_id);
            $this->success('删除成功');
        } else {
            $this->error('非法请求');
        }
    }

    // 批量更新菜单
    public function save_allmenu()
    {
        $post = I('post.');
        if (count($post['id']) > 0) {
            foreach ($post['id'] as $key => $id) {
                $data['id']   = $post['id'][$key];
                $data['name'] = $post['name'][$key];
                $data['sort'] = $post['sort'][$key];
                D('WeixinMenu')->save($data);
            }
            $this->success('保存成功');
        } else {
            $this->error('没有数据保存');
        }
    }

    // 同步公众号菜单
    public function sync_mpmenu()
    {
        $ret = Weixin::menu_sync();
        if ($ret === true) {
            $this->success('同步成功');
        } else {
            $this->error($ret);
        }
    }

    // 微信模板消息ID管理
    public function msgtpl()
    {
        $list = D('WeixinMsgtpl')->select();
//        dump($list);
        $this->assign('list', $list);
        $this->display();
    }

    // 微信模板消息ID保存
    public function msgtpl_save()
    {
        $postData = I('post.');
        $dataList = array_form_input($postData);

        foreach ($dataList as $save) {
            D('WeixinMsgtpl')->save($save);
        }

        $this->success('保存成功');
    }
}